package com.syncshot.androidapp.activities;

import java.util.Timer;
import java.util.TimerTask;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import com.syncshot.androidapp.Constant;
import com.syncshot.androidapp.R;

public class PictureWaitUploadActivity extends AbstractActivity {
    private static final String TAG = "PictureWaitUploadActivity";

    private ImageView           mImageView;

    private BroadcastReceiver   mPictureTakenBroadcastReceiver;
    
    private Handler mHandler = new Handler();
    private Runnable mNextScreen = new Runnable() {
        @Override
        public void run() {
            nextScreen();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_upload);
        mImageView = (ImageView) findViewById(R.id.imageview);

        Uri uri = getIntent().getData();

        mImageView.setImageURI(uri);

        mPictureTakenBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.v(TAG, "Start PictureList");
                nextScreen();
            }
        };
        Log.v(TAG, "registerReceiver");
        registerReceiver(mPictureTakenBroadcastReceiver, new IntentFilter(Constant.BROADCAST_PICTURE_TAKEN));

        mHandler.postDelayed(mNextScreen, 4000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mPictureTakenBroadcastReceiver != null) {
            unregisterReceiver(mPictureTakenBroadcastReceiver);
        }
        mHandler.removeCallbacks(mNextScreen);
    }
    
    
    private void nextScreen() {
        Intent intentToSend = new Intent(this, PictureListResultActivity.class);
        String eventId = getIntent().getStringExtra(Constant.KEY_EVENT_ID);
        intentToSend.putExtra(Constant.KEY_EVENT_ID, eventId);
        startActivity(intentToSend);
        finish();
    }

}
